package com.example.deliveryapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {
    private List<Menu> menuList;
    private OnQuantityChangeListener listener;

    // 添加一个公开的方法来返回 menuList
    public List<Menu> getMenuList() {
        return menuList;
    }

    public interface OnQuantityChangeListener {
        void onQuantityChanged(Menu menu);
    }

    public MenuAdapter(List<Menu> menuList, OnQuantityChangeListener listener) {
        this.menuList = menuList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_menu, parent, false);
        return new MenuViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuViewHolder holder, int position) {
        Menu menu = menuList.get(position);
        holder.itemName.setText(menu.getName());
        holder.itemDescription.setText(menu.getDescription());
        holder.itemImage.setImageResource(menu.getImageResourceId());
        holder.itemQuantity.setText(String.valueOf(menu.getSelectedQuantity()));
        holder.itemPrice.setText(String.format("$%.2f", menu.getPrice()));

        // 绑定增加和减少按钮的点击事件
        holder.decreaseButton.setOnClickListener(v -> {
            menu.setSelectedQuantity(Math.max(0, menu.getSelectedQuantity() - 1));
            notifyItemChanged(getMenuList().indexOf(menu)); // 更新列表索引
            listener.onQuantityChanged(menu);
        });

        holder.increaseButton.setOnClickListener(v -> {
            menu.setSelectedQuantity(menu.getSelectedQuantity() + 1);
            notifyItemChanged(getMenuList().indexOf(menu)); // 更新列表索引
            listener.onQuantityChanged(menu);
        });
    }

    @Override
    public int getItemCount() {
        return menuList.size();
    }

    public class MenuViewHolder extends RecyclerView.ViewHolder {
        ImageView itemImage;
        TextView itemName;
        TextView itemDescription;
        TextView itemQuantity;
        ImageButton decreaseButton;
        ImageButton increaseButton;
        TextView itemPrice;

        public MenuViewHolder(@NonNull View itemView) {
            super(itemView);
            itemImage = itemView.findViewById(R.id.item_image);
            itemName = itemView.findViewById(R.id.item_name);
            itemDescription = itemView.findViewById(R.id.item_description);
            itemQuantity = itemView.findViewById(R.id.item_quantity);
            decreaseButton = itemView.findViewById(R.id.decrease_button);
            increaseButton = itemView.findViewById(R.id.increase_button);
            itemPrice = itemView.findViewById(R.id.item_price);
        }
    }

}
